package Demo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2022-11-25
 * Time: 16:00
 */
public class ThreadDemo2 {

    public static void main(String[] args) throws InterruptedException {
        Thread[] t = new Thread[20];  // 存储对应20个线程
        for (int i = 0; i < t.length; i++) {
            //内部类中使用但未声明的任何局部变量必须在内部类的正文之前明确分配
            final int j = i;
            t[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(j);
                }
            });
        }

        for (Thread thread : t) {
            // 启动线程
            thread.start();
            // 阻塞等待
            thread.join();

        }

        System.out.println("OK");

    }
}
